package com.yz.food.cms.controller;

import com.yz.food.cms.common.ServerResponse;
import com.yz.food.cms.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FileController {
    @Autowired
    private FileService fileService;
    /**
     * 上传图片
     */
    @PostMapping("file/upload")
    @ResponseBody
    public ServerResponse upload(@RequestParam(value = "editImage", required = false) MultipartFile editImage,
                                 HttpServletRequest request) {
        String path = request.getSession(true).getServletContext().getRealPath("upload");
        String upload =fileService.upload(editImage, path);
        return ServerResponse.createBySuccess("上传成功", upload);
    }
}
